package com.component;

import com.alibaba.fastjson.JSON;
import com.domain.KafkaMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;


import java.util.Date;

@Component
@Slf4j
public class KafkaProducer {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    public void send(String msg) {
        KafkaMsg KafkaMsg = new KafkaMsg();
        KafkaMsg.setId(System.currentTimeMillis());
        KafkaMsg.setMsg(msg);
        KafkaMsg.setSendTime(new Date());
        log.info("【生产者发送消息：{}】", JSON.toJSONString(KafkaMsg));
        //对 topic = hello2 的发送消息
        kafkaTemplate.send("autotest",JSON.toJSONString(KafkaMsg));

    }


}
